package com.lzhl.buildjar.ui.support;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2016/2/2
 */
public class FileExProvider implements SuggestProvider {
    private boolean onlyDir = false;
    private String rootPath;

    public FileExProvider() {
    }

    public FileExProvider(String rootPath) {
        this.rootPath = rootPath;
    }

    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
    }

    public String getRootPath() {
        return this.rootPath;
    }

    public List<String> suggest(String text) {
        LinkedList<String> suggests = new LinkedList<String>();

        File fRootPath = new File(rootPath);
        if(!fRootPath.isDirectory()) {
            return suggests;
        }
        rootPath = fRootPath.getAbsolutePath();

        File fTmp = new File(text);
        //输入了绝对路径
        if(fTmp.getPath().equals(fTmp.getAbsolutePath())) {
            text = text.trim();
            File file = new File(text);
            File parent;
            if(text.endsWith(File.separator)) {
                parent = file;
            }
            else {
                parent = file.getParentFile();
            }

            if(parent != null) {
                File[] files = parent.listFiles();
                if (files != null) {
                    for (File aFile : files) {
                        if(aFile.getAbsolutePath().toLowerCase().startsWith(text.toLowerCase())) {
                            if(onlyDir) {
                                if(aFile.isDirectory()) {
                                    suggests.addLast(aFile.getAbsolutePath());
                                }
                            }
                            else {
                                suggests.addLast(aFile.getAbsolutePath());
                            }
                        }
                    }
                }
            }
            return suggests;
        }
        else {
            String path =rootPath;
            text = text.trim();
            if(text.length() > 0) {
                path += File.separator + text;
            }
            File file = new File(path);
            File parent;
            if(path.equals(rootPath) || text.endsWith(File.separator)) {
                parent = file;
            }
            else {
                parent = file.getParentFile();
            }

            if(parent != null) {
                File[] files = parent.listFiles();
                if (files != null) {
                    int len = rootPath.length() + File.separator.length();
                    for (File aFile : files) {
                        if(aFile.getAbsolutePath().toLowerCase().startsWith(path.toLowerCase())) {
                            suggests.addLast(aFile.getAbsolutePath().substring(len));
                        }
                    }
                }
            }
            return suggests;
        }
    }

    public static void main(String[] args) {
        FileExProvider provider = new FileExProvider(new File("jar").getAbsolutePath());
        provider.suggest("c");
    }
}
